package com.jieshuibao.util;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.jieshuibao.common.config.Const;
import com.jieshuibao.common.config.Global;

public class MoneyRandom {
	public static final double  concentrated = Double.valueOf(Global.getConfig("concentrated.money"));
	public static final double  variance= Double.valueOf(Global.getConfig("variance.num"));
	public static final double  max = Double.valueOf(Global.getConfig("max.money"));
	
	//每天的偷偷看红包金额前两次
	public static final double  everydayConcentrated = Double.valueOf(Global.getConfig("everyday.concentrated.money"));
	public static final double  everydayVariance= Double.valueOf(Global.getConfig("everyday.variance.num"));
	public static final double  everydayMax = Double.valueOf(Global.getConfig("everyday.max.money"));
	
	
	public static double getMoney(){
		java.text.DecimalFormat   df   =new   java.text.DecimalFormat("#.00");  
		 double a=concentrated;//集中金额
		 double b=variance;//越小越集中
		 double c=max;//最大金额
		 double d = MoneyRandom.Norm_rand(a, b);
		 if(d>c){
			d=c; 
		 }else if(d<a){
			d=a;
		}
		String format = df.format(d);
		Double valueOf = Double.valueOf(format);
		 
		 
              return valueOf;
		
		
		
   
	}
	public static double getEveryDayMoney(){
		java.text.DecimalFormat   df   =new   java.text.DecimalFormat("#.00");  
		 double a=everydayConcentrated;//集中金额
		// double b=everydayVariance;//越小越集中
		 double c=everydayMax;//最大金额
		 double d = randomNum(c,a);
			String format = df.format(d);
			Double valueOf = Double.valueOf(format);
			 
	              return valueOf ;
	
   
	}
	//偷偷看红包
	public static double getTwoMoney(){
		/*double[] a={1.68,2.68,3.68};
		Random r = new Random(); 
		int indexInt = r.nextInt(3);
		return a[indexInt];*/
		
		NumberFormat numberFormat = NumberFormat.getInstance();  
		numberFormat.setMaximumFractionDigits(2); 
			double randomNum = randomNum(0,0.45);
			String format = numberFormat.format(randomNum);
			Double valueOf = Double.valueOf(format);
			if(valueOf==0){
				valueOf=0.01;
			}
		return valueOf;
		
		
	}
	/**
	 * 悬赏采纳后给咨询师分账比例
	 * @param money
	 * @return
	 */
	public static double getSubMoney(double money){
		int a=(int)(100*Const.REWARDSCALE);
		double b=a*money/100;
	   return b;
	}
	//咨询分钱
	public static double getAdvisorySubMoney(double money){
		int a=(int)(100*0.65);
		double b=a*money/100;
	   return b;
	}
	/**
	 * 偷偷成功回答问题的人分钱比例
	 * @param money
	 * @return
	 */
	public static double getSubMoneyFifteen(double money){
		int a=(int)(100*Const.TTKREPLYSCALE);
		double b=a*money/100;
		
	   return b;
	}/**
	 * 偷偷成功提问问题的人分钱比例
	 * @param money
	 * @return
	 */
	public static double getTtkAskMoney(double money){
		int a=(int)(100*Const.TTKASKSCALE);
		double b=a*money/100;
		
	   return b;
	}
	/**
	 * 正态分布
	 * @param miu 期望
	 * @param sigma2  方差
	 * @return
	 */
	public static double Norm_rand(double miu, double sigma2){
		  double N = 12;
		  double x=0,temp=N;
		  do{
		   x=0;
		   for(int i=0;i<N;i++)
		    x=x+(Math.random());
		   x=(x-temp/2)/(Math.sqrt(temp/12));
		   x=miu+x*Math.sqrt(sigma2);
		   }while(x<=0);          //把小于0的数排除掉了
		   return x;
		 }
	
	public static double randomMoney(){
		List<Double> a=new ArrayList<Double>();
		for (int i = 0; i < 50; i++) {
			a.add(3.3);
		}
		for (int i = 50; i < 70; i++) {
			a.add(4.5);
		}
		for (int i = 70; i < 80; i++) {
			a.add(5.5);
		}
		for (int i = 80; i < 90; i++) {
			a.add(6.6);
		}
		for (int i = 90; i < 95; i++) {
			a.add(7.8);
		}
		for (int i = 95; i < 99; i++) {
			a.add(8.8);
		}
		a.add(9.9);
		
		Random r = new Random(); 
		int indexInt = r.nextInt(100);
		return a.get(indexInt);
	}
	
	public static double registAndShareMoney(){
		List<Double> a=new ArrayList<Double>();
		for (int i = 0; i < 50; i++) {
			a.add(1.68);
		}
		for (int i = 50; i < 70; i++) {
			a.add(2.68);
		}
		for (int i = 70; i < 80; i++) {
			a.add(3.68);
		}
		for (int i = 80; i < 90; i++) {
			a.add(4.68);
		}
		for (int i = 90; i < 95; i++) {
			a.add(5.68);
		}
		for (int i = 95; i < 99; i++) {
			a.add(6.68);
		}
		a.add(7.68);
		
		Random r = new Random(); 
		int indexInt = r.nextInt(100);
		return a.get(indexInt);
	}
	public static double comfortMoney(){
		List<Double> a=new ArrayList<Double>();
		a.add(0.66);
		a.add(0.99);
		Random r = new Random(); 
		int indexInt = r.nextInt(2);
		return a.get(indexInt);
	}
	//一天前两次偷偷看红包 存redis 的时间
	public static long getEveryDateTime(){
		Date date = new Date();
		Date date2 = getNextDay(date);
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
				"yyyy-MM-dd");
		long a=0;
		try {
			Date parse = sdf.parse(sdf.format(date2));
			long time = parse.getTime();
			a=time-date.getTime();
			return a;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return a;
	}
	 public static Date getNextDay(Date date) {
	        Calendar calendar = Calendar.getInstance();
	        calendar.setTime(date);
	        calendar.add(Calendar.DAY_OF_MONTH, +1);//+1今天的时间加一天
	        date = calendar.getTime();
	        return date;
	    }
	 //今天的零点
	 public static Date getBeforeDateTime(){
			Date date = new Date();
			java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
					"yyyy-MM-dd");
			try {
				Date parse = sdf.parse(sdf.format(date));
				
				return parse;
				
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return null;
		}
	 //今天的晚上十二点
	 public static Date getAfterDateTime(){
			Date date = new Date();
			Date date2 = getNextDay(date);
			java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
					"yyyy-MM-dd");
			java.text.SimpleDateFormat sdf2 = new java.text.SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			try {
				String format2 = sdf.format(date2);
				
				Date parse = sdf.parse( format2);
				long a=parse.getTime()-1000;
				String format = sdf2.format(a);
				Date parse2 = sdf2.parse(format);
				
				
				return parse2;
				
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return null;
		}
	 public static double randomNum(double max,double min){
		 int x=(int)max*10;
		 int n=(int)min*10;
		 double a = (double)(n+Math.random()*(x-n+1));
		 double b=a/10;
			return b;
		}
	 //新年红包
	public static double newYearRed(){
		List<Double> a=new ArrayList<Double>();
		a.add(0.66);
		a.add(0.88);
		Random r = new Random(); 
		int indexInt = r.nextInt(2);
		return a.get(indexInt);
	}
	
	public static void main(String[] args) {
	
	/*	
		 Calendar todayStart = Calendar.getInstance();  
		 
		 	todayStart.set(Calendar.DAY_OF_MONTH, todayStart.getActualMaximum(Calendar.DAY_OF_MONTH));
	        todayStart.set(Calendar.HOUR_OF_DAY, 0);  
	        todayStart.set(Calendar.MINUTE, 0);  
	        todayStart.set(Calendar.SECOND, 0);  
	        todayStart.set(Calendar.MILLISECOND, 0); 
	        System.out.println(todayStart.getTimeInMillis());
	        Calendar calendar1 = Calendar.getInstance(); 
	       // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
	        try {
				calendar1.setTime(sdf.parse("2015-05-2"));
			} catch (ParseException e) {
				e.printStackTrace();
			}  
	        System.out.println(calendar1.getActualMaximum(Calendar.DAY_OF_MONTH));
	        System.out.println(Calendar.DAY_OF_MONTH);
		*/
		
		for (int i = 0; i < 100; i++) {
			double norm_rand = Norm_rand(3.0,1);
			System.out.println(norm_rand);
			
			//double everyDayMoney = getEveryDayMoney();
			//System.out.println(everyDayMoney);
		}
		
		
		
	}
}
